import i18n from 'i18next'
import {CHANGE_LANGUAGE,ADD_LANGUAGE,languageActionTypes} from "./languageActions"
export interface languageState {
  language: string
  languageList: { name: string; code: string }[]
}

const defaultState: languageState = {
  language: '中文',
  languageList: [
    { name: '中文', code: 'zh' },
    { name: 'English', code: 'en' },
  ],
}

export default (state = defaultState, action: languageActionTypes ) => {
  console.log('action', action)

  switch (action.type) {
    case CHANGE_LANGUAGE:
      i18n.changeLanguage(action.payload)
      return { ...state, language: action.payload }
    case ADD_LANGUAGE:
      // i18n.changeLanguage(action.payload)
      return {
        ...state,
        language: action.payload.name,
        languageList: [...state.languageList, action.payload],
      }
    default:
      return state
  }
}
